/**
 * @activiti 流程测试类
 */
package com.cloudwise.isop.kg.api;

import com.cloudwise.isop.kg.pojo.TaskRepresentation;
import com.cloudwise.isop.kg.service.ProcessService;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/process")
public class ProcessController {

    @Autowired
    private ProcessService processService;

    /**
     * 发布流程，只在第一次启动或者流程有变化时执行一次
     */
    @RequestMapping(value="/deploy", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public void deploy() {
        processService.deploy();
    }

    /**
     * 启动一个流程
     * @param caseId 缺陷案例id
     */
    @RequestMapping(value="/run", method= RequestMethod.GET)
    public void startProcessInstance(Long caseId) {
        processService.startProcess2(caseId);
    }

    /**
     * 根据角色查询任务
     * @param role 角色
     * @return List<TaskRepresentation>
     */
    @RequestMapping(value="/tasks", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public List<TaskRepresentation> getTasks(String role) {
        List<Task> tasks = processService.getTasks(role);
        List<TaskRepresentation> dtos = new ArrayList<TaskRepresentation>();
        for (Task task : tasks) {
            // 获取流程实例id 并 查询关联的 fromdata 数据
            String processInstanceId = task.getProcessInstanceId();
            TaskRepresentation taskRepresentation = new TaskRepresentation(task.getId(), task.getName(), processInstanceId, null);
            // Object formData = formDataService.select(processInstanceId);
            // taskRepresentation.setFormData(formData);
            dtos.add(taskRepresentation);
        }
        return dtos;
    }

    /**
     * 根据用户查询任务
     * @param user 用户名
     * @return List<TaskRepresentation>
     */
    @RequestMapping(value="/tasksByUser", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public List<TaskRepresentation> getTasksByUser(String user) {
        List<Task> tasks = processService.getTasksByUser(user);
        List<TaskRepresentation> dtos = new ArrayList<TaskRepresentation>();
        for (Task task : tasks) {
            // 获取流程实例id 并 查询关联的 fromdata 数据
            String processInstanceId = task.getProcessInstanceId();
            TaskRepresentation taskRepresentation = new TaskRepresentation(task.getId(), task.getName(), processInstanceId, null);
            // Object formData = formDataService.select(processInstanceId);
            // taskRepresentation.setFormData(formData);
            dtos.add(taskRepresentation);
        }
        return dtos;
    }

    /**
     * 审核通过一个流程
     * @param taskId 任务id
     * @param comment 评论
     * @param userId 当前登录用户名
     */
    @RequestMapping(value="/compeleteTask", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public void compeleteTask(String taskId, String comment, String userId) {
        processService.compeleteTask(taskId, comment, userId);
    }

    /**
     * 驳回流程
     * @param taskId 任务id
     * @param comment 评论
     * @param userId 当前登录用户名
     * @param type 驳回类型，主要用来区分驳回路径( uploadCase这个流程中没有用到)
     */
    @RequestMapping(value="/rejectTask", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public void rejectTask(String taskId, String comment, String userId, Integer type) {
        processService.rejectTask(taskId, comment, userId, type);
    }

    /**
     * 查看任务的历史信息
     * @param processInstanceId 任务id
     * @return List<Comment>
     */
    @RequestMapping(value="/history", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public List<Comment> history(String processInstanceId) {
        return processService.history(processInstanceId);
    }

}